<?php
namespace frontend\controllers;

use common\models\Post;
use frontend\models\ApplyForm;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use common\models\Category;
use yii\web\UploadedFile;
use common\WuMing;


/**
 * Logincontroller
 */
class ApplyController extends Controller
{


    public function actionIndex()
    {

        $model = new Category();

        $diff =  WuMing::getUserInfo('online_time')/60  -100;
        if($diff < 0 ){
            return $this->render('index', [
                'model' => $model,
                'msg'=>'在线时间不足100小时，无法申请创建'
            ]);
        }


        if ($model->load(Yii::$app->request->post())) {
            $thumb = UploadedFile::getInstance($model, 'thumb');
            $logo = UploadedFile::getInstance($model, 'logo');
            if ($thumb) {
                $dir = 'uploads/' . date("Ym");
                if (!file_exists($dir)) {
                    mkdir($dir);
                }
                $thumb_path = $dir . "/" . time() . rand(0, 99) . "." . $thumb->getExtension();
                $thumb->saveAs($thumb_path);
                $model->thumb = $thumb_path;

            }
            if ($logo) {
                $dir = 'uploads/' . date("Ym");
                if (!file_exists($dir)) {
                    mkdir($dir);
                }
                $logo_path = $dir . "/" . time() . rand(0, 99) . "." . $logo->getExtension();
                $logo->saveAs($logo_path);
                $model->logo = $logo_path;

            }
            //自动填写
            $model->create_at = time();
            $model->create_by =Yii::$app->user->id;
            $model->status =0;
            $model->sort =100;
            $model->save();
            return $this->render('index', [
                'model' => $model,
                'msg'=>'申请成功,请等待管理员审核'
            ]);
        } else {
            return $this->render('index', [
                'model' => $model,
            ]);
        }
    }





}
